package com.fingard.avatar.website.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fingard.avatar.website.exception.BizException;
import com.fingard.avatar.website.model.dto.*;
import com.fingard.avatar.website.model.enums.DeletedEnum;
import com.fingard.avatar.website.model.po.SysPageBodyPo;
import com.fingard.avatar.website.dao.SysPageBodyMapper;
import com.fingard.avatar.website.service.SysPageBodyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fingard.avatar.website.utils.DaoUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

/**
 *  Service实现类
 *
 * @author 徐一涛
 * @date 2022-05-10
 */
@Slf4j
@Service
public class SysPageBodyServiceImpl extends ServiceImpl<SysPageBodyMapper, SysPageBodyPo> implements SysPageBodyService {

    @Override
    public boolean add(PageBodyAddDto req) {
        SysPageBodyPo bean = getOne(Wrappers.<SysPageBodyPo>lambdaQuery().eq(SysPageBodyPo::getBodyName,req.getBodyName()).
                eq(SysPageBodyPo::getIsDeleted, DeletedEnum.NOT_DELETED.getValue()));
        if (ObjectUtil.isNotNull(bean)) {
            throw BizException.of(String.format("(%s)页面部位已创建",req.getBodyName()));
        }
        bean = new SysPageBodyPo();
        BeanUtils.copyProperties(req, bean);
        DaoUtils.insert(bean);
        return save(bean);
    }

    @Override
    public boolean edit(PageBodyEditDto req) {
        SysPageBodyPo bean = getOne(Wrappers.<SysPageBodyPo>lambdaQuery().eq(SysPageBodyPo::getBodyName,req.getPageName()).
                eq(SysPageBodyPo::getIsDeleted, DeletedEnum.NOT_DELETED.getValue()).notIn(SysPageBodyPo::getId, req.getId()));
        if (ObjectUtil.isNotNull(bean)) {
            throw BizException.of(String.format("(%s)页面部位已创建",req.getPageName()));
        }
        bean = getOne(Wrappers.<SysPageBodyPo>lambdaQuery().eq(SysPageBodyPo::getId,req.getId()).
                eq(SysPageBodyPo::getIsDeleted, DeletedEnum.NOT_DELETED.getValue()));
        if (ObjectUtil.isNull(bean)) {
            throw BizException.of(String.format("id:(%s)页面部位不存在",req.getId()));
        }
        BeanUtils.copyProperties(req, bean, "id");
        DaoUtils.update(bean);
        return updateById(bean);
    }

    @Override
    public boolean delete(IdDto req) {
        SysPageBodyPo bean = getOne(Wrappers.<SysPageBodyPo>lambdaQuery().eq(SysPageBodyPo::getId,req.getId()).
                eq(SysPageBodyPo::getIsDeleted, DeletedEnum.NOT_DELETED.getValue()));
        if (ObjectUtil.isNull(bean)) {
            throw BizException.of(String.format("id:(%s)页面部位不存在",req.getId()));
        }
        DaoUtils.delete(bean);
        return updateById(bean);
    }

    @Override
    public IPage<SysPageBodyPo> page(PageBodyPageDto req) {
        LambdaQueryWrapper<SysPageBodyPo> wrapper = Wrappers.<SysPageBodyPo>lambdaQuery().
                eq(SysPageBodyPo::getIsDeleted,DeletedEnum.NOT_DELETED.getValue()).
                orderByDesc(SysPageBodyPo::getCreateTime);
        if(StrUtil.isNotEmpty(req.getBodyName())) {
            wrapper.like(SysPageBodyPo::getBodyName, req.getBodyName());
        }
        if(ObjectUtil.isNotNull(req.getPageId())) {
            wrapper.eq(SysPageBodyPo::getPageId, req.getPageId());
        }
        return page(new Page<>(req.getPageNum(), req.getPageSize()), wrapper);
    }
}
